package com.wing.cabinet.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.wing.common.utils.HttpHelper;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.platform.order.model.entity.Agent;
import com.wing.platform.order.model.entity.MainboardType;
import com.wing.platform.order.model.entity.Product;
import com.wing.platform.order.model.response.PanelVersionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author
 */
@Slf4j
@Validated
@Api(value = "App-面板版本管理", tags = "面板版本管理")
@RestController("appPanelVersionController")
@RequestMapping("version/app/panelVersion")
public class PanelVersionController {

    @Value("${platform.gatewayUrl}")
    private String url;


    @ApiOperation(value = "查询所有面板版本")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/getAllVersionList")
    public PageResult<PanelVersionVO> getAllVersionList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,@RequestParam(value = "cabCode") String cabCode) {
        try {
            String params = "?cabCode=" + cabCode + "&page=" + page + "&limit=" + limit + "&sort=" + sort + "&order=" + order;
            String s = HttpHelper.httpGet(url + "/platformCloud/version/app/panelVersion/getAllVersionList" + params);
            PageResult<PanelVersionVO> data = JSONObject.parseObject(s, PageResult.class);
            return data;
        }catch (Exception e){
            log.error("getVersion err",e);
            return null;
        }
    }

    /**
     * 通过查询条件查询版本
     */
    @ApiOperation(value = "通过查询条件查询版本")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "产品系列ID", defaultValue = "", example = "", required = true, dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "channelCode", value = "渠道编号", defaultValue = "", example = "", required = true, dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "motherboardTypeId", value="主板类型ID", defaultValue = "", example = "", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/getVersion")
    public JsonResult<PanelVersionVO> getVersion(
            @RequestParam(value = "cabCode") String cabCode)
            //@RequestParam(value = "productId") Long productId
            //, @RequestParam(value = "channelCode") String channelCode
            //, @RequestParam(value = "motherboardTypeId") Long motherboardTypeId)
            {
        try {
            //String params = "?productId=" + productId + "&channelCode=" + channelCode + "&motherboardTypeId=" + motherboardTypeId;
            String s = HttpHelper.httpGet(url + "/platformCloud/version/app/panelVersion/queryCurrentVersion?cabCode=" + cabCode);
            JsonResult<PanelVersionVO> data = JSONObject.parseObject(s, JsonResult.class);
            return data;
        }catch (Exception e){
            log.error("getVersion err",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    /**
     * 获取渠道列表
     */
    @GetMapping("/getAgentList")
    public JsonResult<Agent> getAgentList(){
        try {
            String params = "?limit=99999&status=2,3";
            String s = HttpHelper.httpGet(url + "/platform/branch/app/agent/list" + params);
            JsonResult<Agent> data = JSONObject.parseObject(s, JsonResult.class);
            return data;
        }catch (Exception e){
            log.error("getAgentList err",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    /**
     * 获取刀柜系列列表
     */
    @GetMapping("/getProductList")
    public JsonResult<Product> getProductList(){
        try {
            String params = "?limit=99999&typeEnumCode=D00001";
            String s = HttpHelper.httpGet(url + "/platform/product/app/product/list" + params);
            JsonResult<Product> data = JSONObject.parseObject(s, JsonResult.class);
            return data;
        }catch (Exception e){
            log.error("getProductList err",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    /**
     * 获取主板类型列表
     */
    @GetMapping("/getMotherboardTypeList")
    public JsonResult<MainboardType> getMotherboardTypeList(){
        try {
            String params = "?limit=99999";
            String s = HttpHelper.httpGet(url + "/platform/product/app/mainboardType/list" + params);
            JsonResult<MainboardType> data = JSONObject.parseObject(s, JsonResult.class);
            return data;
        }catch (Exception e){
            log.error("getMotherboardTypeList err",e);
            return JsonResult.fail(e.getMessage());
        }
    }

}
